package com.shanghaionstar.weight;

import android.content.Context;
import android.content.res.TypedArray;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.onstar.cn.common.util.CommonUtil;
import com.onstar.cn.framework.common.CustomLayoutsGroup;
import com.onstar.cn.weight.R;
import com.onstar.cn.weight.discover.ItemViewData;

import java.util.List;

/**
 * Created by 7828wc on 16/6/27.
 */
public class DiscoverItemLayout extends LinearLayout {
    private View view;
    private TextView layout_title;
    private LinearLayout data_layout;
    private List<ItemViewData> itemViews;
    private CustomLayoutsGroup.OnViewItemClickListener onViewItemClickListener;

    public DiscoverItemLayout(Context context) {
        super(context);
        initView(null);
    }

    public DiscoverItemLayout(Context context, AttributeSet attrs) {
        super(context, attrs);
        initView(attrs);
    }

    public DiscoverItemLayout(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        initView(attrs);
    }

    private void initView(AttributeSet attrs) {
        view = inflate(getContext(), R.layout.weight_discover_item_layout, this);
        layout_title = (TextView) view.findViewById(R.id.tv_title);
        data_layout = (LinearLayout) view.findViewById(R.id.discover_data_layout);

        if (attrs != null) {
            TypedArray typedArray = getContext().obtainStyledAttributes(attrs, R.styleable.input);
            String title = typedArray.getString(R.styleable.input_topTitle);
            layout_title.setText(title);


            typedArray.recycle();
        }
    }

    public void showDatas(List<ItemViewData> itemViews) {
        showDatas(itemViews, "");
    }

    /**
     * 根据传进去的数据显示View
     *
     * @param itemViews
     */
    public void showDatas(List<ItemViewData> itemViews, String topTitle) {
        this.itemViews = itemViews;
        if (!CommonUtil.isNull(topTitle))
            layout_title.setText(topTitle);
        if (itemViews != null && itemViews.size() > 0) {
//            for (int i = 0; i < itemViews.size(); i++) {
//                ItemViewData itemView = itemViews.get(i);
//                CustomLineLayout customLineLayout = new CustomLineLayout(getContext());
//                customLineLayout.setImageIcon(itemView.getIcon());
//                customLineLayout.setText(itemView.getText());
//                customLineLayout.setTextColor(R.color.onstar_discover_item_text_color);
//                data_layout.addView(customLineLayout);
//            }
            CustomLayoutsGroup group = new CustomLayoutsGroup(getContext(), itemViews);
            group.setOnViewItemClickListener(onViewItemClickListener);
            data_layout.removeAllViews();
            group.customLineLayoutGroup(data_layout);
        }

    }

    public void performClickByEventTag(String itemText){
        if (itemViews == null || TextUtils.isEmpty(itemText))
            return;
        for (int i = 0,j = itemViews.size(); i < j ; i++) {
            if (itemText.equals(itemViews.get(i).getText())){
                data_layout.getChildAt(i).performClick();
                break;
            }
        }
    }

    public void removeData(String itemText){
        if (itemViews == null || TextUtils.isEmpty(itemText))
            return;
        for (int i = 0,j = itemViews.size(); i < j ; i++) {
            if (itemText.equals(itemViews.get(i).getText())){
                data_layout.removeViewAt(i);
                itemViews.remove(i);
                break;
            }
        }
    }

    public void setOnViewItemClickListener(CustomLayoutsGroup.OnViewItemClickListener onViewItemClickListener) {
        this.onViewItemClickListener = onViewItemClickListener;
    }
}
